package com.test.gpa.security.Impl;

import com.test.gpa.domain.User;
import com.test.gpa.repostitory.MenuSqlRepository;
import com.test.gpa.repostitory.UserRepository;
import com.test.gpa.repostitory.UserSqlRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 重写密码查询
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    UserSqlRepository userSqlRepository;
    @Autowired
    MenuSqlRepository menuSqlRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userSqlRepository.queryByName(username);
        System.out.println(user+"UserDetailsServiceImpl 效验密码");
        if(Objects.isNull(user)){
           throw new RuntimeException("用户名不存在");

        }
        List<String> strings = menuSqlRepository.selectPermsByUserId(user.getId());//将查询到的权限标识符存入用户信息中
        return new LoginUser(user,strings);

    }
}
